package 
{
	import laya.d3.core.Camera;
	import laya.d3.resource.models.SkyBox;
	import laya.d3.core.BaseCamera;
	import laya.d3.resource.TextureCube;
	import laya.d3.resource.models.SkyDome;
	import laya.d3.resource.Texture2D;
	import common.CameraMoveScript;
	import laya.device.motion.Gyroscope;
	import laya.events.Event;
	import laya.device.motion.RotationInfo;
	import laya.d3.math.Vector3;
	import laya.d3.math.Quaternion;

	/**
	 * ...
	 * @author
	 */
	public class GameCamera extends Camera{
		protected var yawPitchRoll:Vector3 = new Vector3();
		protected var tempRotation:Quaternion = new Quaternion();

		public function GameCamera(aspectRatio:Number = 0, nearPlane:Number = 0.3, farPlane:Number = 1000){
			super(aspectRatio,nearPlane,farPlane);
			//创建天空盒
    		var skyBox:SkyBox = new SkyBox();
    		//清除标记，使用天空（必须设置，否者无法显示天空）
    		this.clearFlag=BaseCamera.CLEARFLAG_SKY;
    		//绑定天空盒对象到摄像机
    		this.sky=skyBox;
			skyBox.textureCube=TextureCube.load("res/model/Assets/Skyboxes/Day_BlueSky_Nothing.ltc");
			useGyroscope(true);
			//this.addComponent(CameraMoveScript);
		}

		public function useGyroscope(value:Boolean):void
		{
			if(value)
				Gyroscope.instance.on(Event.CHANGE, this, onDeviceorientation);
			else
				Gyroscope.instance.off(Event.CHANGE, this, onDeviceorientation);
		}

		private function onDeviceorientation(absolute:Boolean, rotationInfo:RotationInfo):void 
		{
			// GameManager.getInstance().setLog("alpha:" + Math.floor(rotationInfo.alpha) + '\n' +
 			// "beta :" + Math.floor(rotationInfo.beta) + '\n' +
 			// "gamma:" + Math.floor(rotationInfo.gamma));
			 var yprElem:Float32Array = yawPitchRoll.elements;
			yprElem[0] -= Math.floor(rotationInfo.beta)/180 * Math.PI;
			yprElem[1] -= Math.floor(rotationInfo.gamma)/180 * Math.PI;
			Quaternion.createFromYawPitchRoll(yprElem[0], yprElem[1], yprElem[2], tempRotation);
			Quaternion.add(tempRotation,this.transform.localRotation,tempRotation);
			this.transform.localRotation = tempRotation;
		}
	}

}